---
- name: ensure packages required for buildmaster are installed (yum)
  yum: name={{ item }} state=latest enablerepo={{ extra_enablerepos }}
  with_items:
    - buildbot
    - buildbot-master
    - libsemanage-python
    - python-psycopg2
  when: ansible_distribution_major_version|int < 22

- name: ensure packages required for buildmaster are installed (dnf)
  dnf: name={{ item }} state=latest enablerepo={{ extra_enablerepos }}
  with_items:
    - buildbot
    - buildbot-master
    - libsemanage-python
    - python-psycopg2
  when: ansible_distribution_major_version|int > 21 and ansible_cmdline.ostree is not defined

- name: ensure additional packages required for CI buildmaster are installed
  yum: name={{ item }} state=latest enablerepo={{ extra_enablerepos }}
  when: ansible_distribution_major_version|int < 22 and (deployment_type == 'qadevel-prod' or deployment_type == 'qa-stg')
  with_items:
    - python-novaclient

- name: ensure additional packages required for CI buildmaster are installed
  dnf: name={{ item }} state=latest enablerepo={{ extra_enablerepos }}
  when: ansible_distribution_major_version|int > 21 and (deployment_type == 'qadevel-prod' or deployment_type == 'qa-stg')
  with_items:
    - python-novaclient

- name: add the buildmaster user
  user: name={{ buildmaster_user }} home={{ buildmaster_home }}


- name: ensure buildmaster user can use home directory
  file: path="{{ buildmaster_home }}" state=directory owner=buildmaster group=buildmaster mode=0775 setype=user_home_t
  when: deployment_type == 'prod' or deployment_type == 'stg' or deployment_type == 'dev' or deployment_type == 'local'

- name: allow httpd tcp connections with selinux
  seboolean: name=httpd_can_network_connect state=true persistent=yes

- name: copy httpd config
  template: src=buildmaster.conf.j2 dest=/etc/httpd/conf.d/buildmaster.conf owner=root group=root
  notify:
    - reload httpd

- name: copy artifacts httpd config
  template: src=artifacts.conf.j2 dest=/etc/httpd/conf.d/artifacts.conf owner=root group=root
  when: deployment_type == 'prod' or deployment_type == 'stg' or deployment_type == 'dev' or deployment_type == 'local'
  notify:
    - reload httpd

- name: create artifacts directory
  file: path={{ item }} state=directory owner=buildmaster group=buildmaster mode=0775 setype=httpd_sys_content_t
  when: deployment_type == 'prod' or deployment_type == 'stg' or deployment_type == 'dev' or deployment_type == 'local'
  with_items:
  - /srv/taskotron
  - /srv/taskotron/artifacts

- name: ensure buildmaster database is created
  when: buildmaster_db_host != 'localhost'
  delegate_to: "{{ buildmaster_db_host }}"
  sudo: true
  sudo_user: postgres
  action: postgresql_db db={{ buildmaster_db_name }}

- name: ensure dev db user has access to dev database
  when: deployment_type == 'dev'
  delegate_to: "{{ buildmaster_db_host }}"
  sudo: true
  sudo_user: postgres
  action: postgresql_user db={{ buildmaster_db_name }} user={{ dev_buildmaster_db_user }} password={{ dev_buildmaster_db_password }} role_attr_flags=NOSUPERUSER

- name: ensure stg db user has access to stg database
  when: deployment_type == 'stg'
  delegate_to: "{{ buildmaster_db_host }}"
  sudo: true
  sudo_user: postgres
  action: postgresql_user db={{ buildmaster_db_name }} user={{ stg_buildmaster_db_user }} password={{ stg_buildmaster_db_password }} role_attr_flags=NOSUPERUSER

- name: ensure prod db user has access to prod database
  when: deployment_type == 'prod'
  delegate_to: "{{ buildmaster_db_host }}"
  sudo: true
  sudo_user: postgres
  action: postgresql_user db={{ buildmaster_db_name }} user={{ prod_buildmaster_db_user }} password={{ prod_buildmaster_db_password }} role_attr_flags=NOSUPERUSER

- name: ensure local db user has access to local database
  when: deployment_type == 'local'
  delegate_to: "{{ buildmaster_db_host }}"
  sudo: true
  sudo_user: postgres
  action: postgresql_user db={{ buildmaster_db_name }} user={{ local_buildmaster_db_user }} password={{ local_buildmaster_db_password }} role_attr_flags=NOSUPERUSER

- name: put robots.txt at web root of external hostname
  when: robots_path is defined
  template: src=robots.txt.j2 dest={{ robots_path }}/robots.txt owner=apache group=apache mode=0644
